/**
 * com.chaeron.platform
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 * @author      Andrzej Jan Taramina, Chaeron Corporation
 *              andrzej@chaeron.com
 */

package com.chaeron.platform.common;

public class SerialPortError
{
	//***************************************************************************
	//*
	//* 	Error definitions
	//*
	//***************************************************************************/
	
	// Serial Port errors
	
	public static final short			ERROR_BASE					= -1;
	public static final short 			ERROR_UNKNOWN				= ERROR_BASE;
	public static final short 			ERROR_BADPARAM				= ERROR_BASE - 0x0001;
	public static final short 			ERROR_BADPORT				= ERROR_BASE - 0x0002;
	public static final short 			ERROR_NOMEM					= ERROR_BASE - 0x0003;
	public static final short 			ERROR_BADCONNID				= ERROR_BASE - 0x0004;
	public static final short 			ERROR_TIMEOUT				= ERROR_BASE - 0x0005;
	public static final short 			ERROR_LINEERR				= ERROR_BASE - 0x0006;
	public static final short 			ERROR_ALREADYOPEN			= ERROR_BASE - 0x0007;
	public static final short 			ERROR_STILLOPEN				= ERROR_BASE - 0x0008;
	public static final short 			ERROR_NOTOPEN				= ERROR_BASE - 0x0009;
	public static final short 			ERROR_NOTSUPPORTED			= ERROR_BASE - 0x000A;		
	public static final short 			ERROR_NODEVICESAVAIL		= ERROR_BASE - 0x000B;	
	public static final short 			ERROR_CONFIGURATIONFAILED	= ERROR_BASE - 0x000C;
	public static final short			ERROR_READCHECKMISMATCH		= ERROR_BASE - 0x000D;
	public static final short			ERROR_WRITECOUNTMISMATCH	= ERROR_BASE - 0x000E;
	
	public static final short			ERROR_LAST					= ERROR_WRITECOUNTMISMATCH;
	
	private static final String[]		messages					=	{
		"Unknown Error",
		"Bad Parameter",
		"Bad Port",
		"No Memory",
		"Bad Connection ID",
		"Timeout",
		"Line Error: ",
		"Port Already Open",
		"Port Still Open",
		"Port Not Open",
		"Not Supported",
		"No Device Available",
		"Configuration Failed",
		"Read Check Mismatch",
		"Write Count Mismatch"
	};
	
	//
	// mask values for the line Errors
	//
	
	public static final short			LINE_ERROR_PARITY			= 0x0001;			// parity error
	public static final short			LINE_ERROR_HWOVERRUN		= 0x0002;			// HW overrun
	public static final short			LINE_ERROR_FRAMING			= 0x0004;			// framing error
	public static final short			LINE_ERROR_BREAK			= 0x0008;			// break signal asserted
	public static final short			LINE_ERROR_HSHAKE			= 0x0010;			// line hand-shake error
	public static final short			LINE_ERROR_SWOVERRUN		= 0x0020;			// HW overrun
	public static final short			LINE_ERROR_CARRIERLOST		= 0x0040;			// CD dropped
	
	private final static char[] HEX_CHARS = {	'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F'};
	
	
	//***************************************************************************
	//*
	//* 	Static methods
	//*
	//***************************************************************************/
	
	public static String getErrorMessage( int error, short lineErr )
	{
		String	msg = messages[ 0 ];
		
		if( error <= ERROR_BASE && error >= ERROR_LAST ) {
			msg = messages[ -error + ERROR_BASE ];
			
			if( error == ERROR_LINEERR ) {
				msg += "0x" + (char)HEX_CHARS[ ( lineErr & 0xf0 ) >>> 4 ] + (char)HEX_CHARS[ lineErr & 0x0f ];
			}
		}
		
		return( msg );
	}
	
}